package com.ruoyi.dish.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.dish.domain.DishFlavor;
import com.ruoyi.system.api.RemoteDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.dish.domain.Dish;
import com.ruoyi.dish.service.IDishService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 菜品管理Controller
 * 
 * @author ruoyi
 * @date 2022-04-22
 */
@RestController
@RequestMapping("/dish")
public class DishController extends BaseController
{
    @Autowired
    private IDishService dishService;

    @Autowired
    private RemoteDishService remoteDishService;

    /**
     * 查询菜品管理列表
     */
    @RequiresPermissions("dish:dish:list")
    @GetMapping("/list")
    public TableDataInfo list(Dish dish)
    {
        startPage();
        List<Dish> list = dishService.selectDishList(dish);
        return getDataTable(list);
    }

    /**
     * 导出菜品管理列表
     */
    @RequiresPermissions("dish:dish:export")
    @Log(title = "菜品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dish dish)
    {
        List<Dish> list = dishService.selectDishList(dish);
        ExcelUtil<Dish> util = new ExcelUtil<Dish>(Dish.class);
        util.exportExcel(response, list, "菜品管理数据");
    }

    /**
     * 获取菜品管理详细信息
     */
    @RequiresPermissions("dish:dish:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Dish dish = dishService.selectDishById(id);
        for (DishFlavor flavor : dish.getDishFlavorList()) {
            String str = flavor.getValue().substring(1,flavor.getValue().length()-1).replaceAll("\"","");
            List<String> list = Arrays.asList(str.split(","));
            flavor.setFlavorValue((String[]) list.toArray());
        }
        return AjaxResult.success(dish);
    }

    /**
     * 新增菜品管理
     */
    @RequiresPermissions("dish:dish:add")
    @Log(title = "菜品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dish dish)
    {
        return toAjax(dishService.insertDish(dish));
    }

    /**
     * 修改菜品管理
     */
    @RequiresPermissions("dish:dish:edit")
    @Log(title = "菜品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dish dish)
    {
        return toAjax(dishService.updateDish(dish));
    }

    /**
     * 删除菜品管理
     */
    @RequiresPermissions("dish:dish:remove")
    @Log(title = "菜品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dishService.deleteDishByIds(ids));
    }

    /**
     * 修改菜品状态
     */
//    @RequiresPermissions("dish:dish:remove")
    @Log(title = "菜品管理", businessType = BusinessType.DELETE)
    @PutMapping("/{ids}/{status}")
    public AjaxResult statusStop(@PathVariable("ids") Long[] ids,@PathVariable("status") Integer status)
    {
        return toAjax(dishService.statusStopByIds(ids,status));
    }

    /**
     *  分类 Id 查询菜品管理列表
     */
//    @RequiresPermissions("dish:dish:list")
    @GetMapping("/cid/{categoryId}")
    public AjaxResult selectDishListByCategoryId(@PathVariable("categoryId") Long categoryId)
    {
        return AjaxResult.success(dishService.selectDishListByCategoryId(categoryId));
    }

    /**
     *  菜品名称 查询菜品列表
     */
//    @RequiresPermissions("dish:dish:list")
    @GetMapping("/dishName/{dishName}")
    public AjaxResult selectDishListByDishName(@PathVariable("dishName") String dishName)
    {
        return AjaxResult.success(dishService.selectDishListByDishName(dishName));
    }

    /**
     * 获取分类列表
     * @return
     */
//    @RequiresPermissions("dish:dish:list")
    @GetMapping("/getCategorylistAll")
    public AjaxResult getCategorylistAll(){
        return remoteDishService.getCategorylistAll();
    }

}
